package com.qf.business.system.controller;

import com.qf.business.system.power.Auth;
import com.qf.business.system.service.DepartmentService;
import com.qf.data.base.r.R;
import com.qf.data.base.r.Rs;
import com.qf.data.system.entity.Department;
import com.qf.data.system.vo.input.DepartmentInput;
import com.qf.data.system.vo.output.DepartmentOutput;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/dep")
public class DepartmentController {

    @Autowired
    private DepartmentService departmentService;

    @Auth(mustLogin = false)
    @RequestMapping("/list")
    public R depList(){
        List<Department> departments = departmentService.list();

        List<DepartmentOutput> outputs = departments.stream()
                .map(department -> {
                    DepartmentOutput departmentOutput = new DepartmentOutput();
                    BeanUtils.copyProperties(department,departmentOutput);
                    return  departmentOutput;
                }).collect(Collectors.toList());
        //返回部门列表
        return Rs.create(outputs);
    }

    /**
     * 新增部门
     * @param departmentInput
     * @return
     */
    @RequestMapping("/insert")
    public R insert(@Validated DepartmentInput departmentInput){
        //将input -> entity
        Department department = new Department();
        BeanUtils.copyProperties(departmentInput,department);
        //进行保存
        departmentService.save(department);
        return Rs.create("succ");
    }


    @RequestMapping("/delete")
    public R delete(Department department){
        departmentService.removeById(department);
        return Rs.create("succ");
    }


}
